package at.jku.mc.database;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public final class TaskProvider {
	private static final int TASK_CHECKED = 1;
	private static final int TASK_UNCHECKED = 0;

	// Database fields
	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;
	private UserProvider userProvider;

	public TaskProvider(Context context) {
		dbHelper = new DatabaseHelper(context);
		userProvider = new UserProvider(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
		if (!database.isReadOnly()) {
			// Enable foreign key constraints
			database.execSQL("PRAGMA foreign_keys=ON;");
		}
	}

	public void close() {
		dbHelper.close();
	}

	public void createTask(String name, String description, long priority,
			boolean redo, long date, long list) {
		long created = Calendar.getInstance().getTimeInMillis();
		ContentValues values = new ContentValues();

		values.put(DatabaseHelper.TASK_COLUMN_NAME, name);
		values.put(DatabaseHelper.TASK_COLUMN_DESCRIPTION, description);
		values.put(DatabaseHelper.TASK_COLUMN_PRIORITY, priority);
		values.put(DatabaseHelper.TASK_COLUMN_REDO, redo ? 1 : 0);
		values.put(DatabaseHelper.TASK_COLUMN_CREATED, created);
		values.put(DatabaseHelper.TASK_COLUMN_DEADLINE, date);
		values.put(DatabaseHelper.TASK_COLUMN_LIST, list);
		long insertId = database
				.insert(DatabaseHelper.TABLE_TASK, null, values);
		Cursor cursor = database.query(DatabaseHelper.TABLE_TASK, null,
				DatabaseHelper.TASK_COLUMN_ID + " = " + insertId, null, null,
				null, null);
		cursor.moveToFirst();
		cursor.close();
	}

	public int setChecked(long id) {
		System.out.println("Task checked with id: " + id);
		ContentValues args = new ContentValues();
		// get Points from priority
		userProvider.addPoints(getPriority(id));
		if (fiveTasksChecked()) {
			userProvider.addFiveTasksDoneBadge();
		}
		args.put(DatabaseHelper.TASK_COLUMN_CHECKED, TASK_CHECKED);
		return database.update(DatabaseHelper.TABLE_TASK, args,
				DatabaseHelper.TASK_COLUMN_ID + " = " + id, null);
	}

	// TODO: Rename to task done
	private boolean fiveTasksChecked() {
		Cursor cursor = database.query(DatabaseHelper.TABLE_TASK,
				new String[] { "sum(" + DatabaseHelper.TASK_COLUMN_CHECKED
						+ ")" }, null, null, null, null, null);
		if (cursor.moveToFirst())
			Log.d("asdf", "move to first");
			if (cursor.getInt(cursor
					.getColumnIndex("sum("+DatabaseHelper.TASK_COLUMN_CHECKED+")")) >= 5) {
				return true;
			}
		return false;
	}

	public int setUnChecked(long id) {
		System.out.println("Task unchecked with id: " + id);
		ContentValues args = new ContentValues();
		userProvider.removePoints(getPriority(id));
		args.put(DatabaseHelper.TASK_COLUMN_CHECKED, TASK_UNCHECKED);
		return database.update(DatabaseHelper.TABLE_TASK, args,
				DatabaseHelper.TASK_COLUMN_ID + " = " + id, null);
	}

	private int getPriority(long id) {
		userProvider.open();
		int value = 0;
		Cursor cursor = database.query(DatabaseHelper.TABLE_TASK,
				new String[] { DatabaseHelper.TASK_COLUMN_PRIORITY },
				DatabaseHelper.TASK_COLUMN_ID + " = " + id, null, null, null,
				null);
		if (cursor.moveToFirst())
			value = cursor.getInt(cursor
					.getColumnIndex(DatabaseHelper.TASK_COLUMN_PRIORITY));
		cursor.close();
		return value;
	}

	// TODO restore checked tasks on fragment opening
	// public Cursor getChecked() {
	// Cursor cursor = database.query(DatabaseHelper.TABLE_TASK, allColumns,
	// DatabaseHelper.TASK_COLUMN_ID + " = " + insertId, null, null,
	// null, null);
	// return cursor;
	// }

	public int getCheckedCount() {
		Cursor cursor = database.query(DatabaseHelper.TABLE_TASK, null,
				DatabaseHelper.TASK_COLUMN_CHECKED + " = " + 1, null, null,
				null, null);
		return cursor.getCount();
	}

	public void deleteTask(long id) {
		System.out.println("Task deleted with id: " + id);
		database.delete(DatabaseHelper.TABLE_TASK,
				DatabaseHelper.TASK_COLUMN_ID + " = " + id, null);
	}

	public Cursor getTasksOfList(long id) {
		// DESC -> hoechste id zuerst anzeigen
		// TODO: nicht hoechste id, sondern Reihenfolge festlegbar
		Cursor cursor = database.query(DatabaseHelper.TABLE_TASK, null,
				DatabaseHelper.TASK_COLUMN_LIST + " = " + id, null, null, null,
				DatabaseHelper.TASK_COLUMN_ID + " DESC");
		return cursor;
	}
}
